package com.knight.util;

import com.knight.model.EntityDataModel;

import java.sql.JDBCType;

/**
 * mysql数据类型转为java数据类型
 * @author suxin
 */
public class MysqlDataType2JavaDataType {

    /**
     * 转成java的数据类型
     * @param entityDataModel
     */
    public static void toJavaDataType(EntityDataModel entityDataModel) {

        for (EntityDataModel.Attributes attributes : entityDataModel.getAttributesList()) {
            JDBCType jdbcType = attributes.getJdbcType();
            if (JDBCType.VARCHAR == jdbcType || JDBCType.NVARCHAR == jdbcType
                    || JDBCType.NCHAR == jdbcType || JDBCType.CHAR == jdbcType
                    || JDBCType.LONGNVARCHAR == jdbcType || JDBCType.LONGVARCHAR == jdbcType) {
                attributes.setFieldType("String");
            }
            if (JDBCType.BIGINT == jdbcType) {
                attributes.setFieldType("Long");
            }
            if (JDBCType.INTEGER == jdbcType || JDBCType.TINYINT == jdbcType || JDBCType.BIT == jdbcType || JDBCType.SMALLINT == jdbcType) {
                attributes.setFieldType("Integer");
            }
            if (JDBCType.FLOAT == jdbcType) {
                attributes.setFieldType("Float");
            }
            if (JDBCType.DOUBLE == jdbcType) {
                attributes.setFieldType("Double");
            }
            if (JDBCType.NUMERIC == jdbcType || JDBCType.DECIMAL == jdbcType) {
                attributes.setFieldType("BigDecimal");
                if(!entityDataModel.getImportPackageList().contains("java.math.BigDecimal")) {
                    entityDataModel.getImportPackageList().add("java.math.BigDecimal");
                }
            }
            if (JDBCType.DATE == jdbcType || JDBCType.TIMESTAMP == jdbcType || JDBCType.TIME == jdbcType) {
                attributes.setFieldType("Date");
                if(!entityDataModel.getImportPackageList().contains("java.util.Date")) {
                    entityDataModel.getImportPackageList().add("java.util.Date");
                }
            }
            if (JDBCType.BOOLEAN == jdbcType) {
                attributes.setFieldType("Boolean");
            }
            if (attributes.getFieldType() == null || "".equals(attributes.getFieldType())) {
                System.out.println("有未对应的数据类型可以进行映射, jdbcType:"+attributes.getJdbcType()+";请联系knight");
                attributes.setFieldType("DDDD");
            }
        }

    }
}
